package com.controller;

import com.entity.FUser;
import com.entity.table.FUserTableDef;
import com.mapper.FUserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.service.impl.FUserServiceImpl;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class LoginController {

    @Autowired
//    private FUserMapper fUserMapper;
    private FUserServiceImpl fUserService;

    @GetMapping("/login")
    public String toLoginPage() {
        return "client/login";
    }

    @PostMapping("/toLogin")
    public String login(@ModelAttribute FUser fUser, HttpSession session) {
//        FUser res = fUserMapper.login(fUser);
        FUser res = fUserService.selectFUser(fUser);
        System.out.println(res);
        /*
        if (res == null) {
            return toLoginPage();
        } else {
//            return "client/index";
            return "redirect:";
        }
         */
        if (res == null) {
            return toLoginPage();
        } else {
            session.setAttribute("user", res); //设置登录信息
//            return "client/index";
            return "redirect:";
        }
    }

    @GetMapping("/register")
    public String toRegisterPage() {
        return "client/register";
    }

    @PostMapping("/toRegister")
    public String register(@ModelAttribute FUser fUser) {
        fUser.setStatus(1); //默认设置1
        fUser.setPhone("no Phone !");
        int code = -1;
        if (fUser.getUserName() != null && fUser.getPwd() != null) {
//            code = fUserMapper.register(fUser);
            code = fUserService.getMapper().insert(fUser);
        }
        System.out.println(fUser + " code = " + code);
        if (code == 1) {
            return "client/login";
        } else {
            return "client/register";
        }
    }

}
